package com.leapp.sk.ui.activity.login

import android.os.Bundle
import android.os.CountDownTimer
import android.text.TextUtils
import android.view.View
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.binder.login.MobileVerificationBinder
import com.leapp.sk.util.ChangePhoneInterfaceUtils
import com.example.util.finallist.CHANGE_PHONE

import com.leapp.sk.viewdelegate.login.MobileVerificationDelegate

/**
 * 手机号验证
 * @author  chaohaipeng  on 2018/7/10.
 */
class MobileVerificationActivity : BaseActivityMVP<MobileVerificationDelegate>() {
    /**
     * 计时器
     */
    private var timeCount: TimeCount? = null

    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            if (!TextUtils.isEmpty(viewDelegate?.verificationCodeCheck())) {
                timeCount?.start()
            }
        }, R.id.tv_amv_get_code)
        viewDelegate.setOnClickListener(View.OnClickListener {
            viewDelegate.clearContent()
        }, R.id.iv_amv_clear_content)

        viewDelegate.setOnClickListener(View.OnClickListener {
            if (!TextUtils.isEmpty(viewDelegate.verificationCodeCheck()) && !TextUtils.isEmpty(viewDelegate.loginCheck())) {
                val mChangePhoneInterface = ChangePhoneInterfaceUtils.getInstance().mChangePhoneInterface
                mChangePhoneInterface?.getPhone(viewDelegate.verificationCodeCheck())
                SKApplication.getInstance()?.exit(2)
            }
        }, R.id.tv_amv_next)
    }

    override fun initView(p0: Bundle?) {
        notifyModelChanged(resources.getString(R.string.string_change_phone))
        viewDelegate.setClearDatavisible()
    }

    override fun initDate() {
        val stringExtra = resources.getString(R.string.string_change_current_phone) + intent?.getStringExtra(CHANGE_PHONE)
        viewDelegate.setCurrentPhone(stringExtra)
        //构造CountDownTimer对象
        timeCount = TimeCount(60000, 1000)
    }


    override fun getDelegateClass(): Class<MobileVerificationDelegate> {
        return MobileVerificationDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {

        return MobileVerificationBinder()
    }

    /**
     * 验证码倒计时
     */
    internal inner class TimeCount(millisInFuture: Long, countDownInterval: Long)//参数依次为总时长,和计时的时间间隔
        : CountDownTimer(millisInFuture, countDownInterval) {
        override fun onFinish() {
            //计时完毕时触发
            viewDelegate?.verificationCode(0)
        }

        override fun onTick(millisUntilFinished: Long) {
            // 计时过程显示
            viewDelegate?.verificationCode(millisUntilFinished)
        }
    }
}